package com.module.admin.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.app.dao.AppApiTestDtlDao;
import com.module.admin.app.pojo.AppApiTest;
import com.module.admin.app.pojo.AppApiTestDtl;
import com.module.admin.app.pojo.AppApiTestLog;
import com.module.admin.app.pojo.AppUser;
import com.module.admin.app.service.AppApiTestDtlService;
import com.module.admin.app.service.AppApiTestLogService;
import com.module.admin.app.service.AppApiTestService;
import com.module.admin.app.service.AppUserService;
import com.module.admin.app.utils.ApiTestUtil;
import com.module.admin.prj.pojo.PrjApi;
import com.module.admin.prj.service.PrjApiService;
import com.module.admin.prj.service.PrjInfoService;
import com.module.admin.sys.pojo.SysUser;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.comm.utils.FrameTimeUtil;
import com.system.handle.model.ResponseFrame;

/**
 * app_api_test_dtl的Service
 * @author admin
 * @date 2020-12-28 16:57:41
 * @version V1.0.0
 */
@Component
public class AppApiTestDtlServiceImpl implements AppApiTestDtlService {

	@Autowired
	private AppApiTestDtlDao appApiTestDtlDao;
	@Autowired
	private AppApiTestService appApiTestService;
	@Autowired
	private AppApiTestLogService appApiTestLogService;
	@Autowired
	private AppUserService appUserService;
	@Autowired
	private PrjInfoService prjInfoService;
	@Autowired
	private PrjApiService prjApiService;
	@Autowired
	private ApiTestUtil apiTestUtil;
	
	/**
	 * 保存或修改
	 * @param appApiTestDtl
	 * @return
	 */
	@Override
	public ResponseFrame saveOrUpdate(AppApiTestDtl appApiTestDtl) {
		ResponseFrame frame = new ResponseFrame();
		if(FrameStringUtil.isEmpty(appApiTestDtl.getAatdId())) {
			if (appApiTestDtl.getStatus() == null) {
				appApiTestDtl.setStatus(10);
			}
			appApiTestDtl.setAatdId(FrameNoUtil.uuid());
			appApiTestDtlDao.save(appApiTestDtl);
		} else {
			appApiTestDtlDao.update(appApiTestDtl);
		}
		frame.setSucc();
		return frame;
	}
	
	/**
	 * 根据aatdId获取对象
	 * @param aatdId
	 * @return
	 */
	@Override
	public AppApiTestDtl get(String aatdId) {
		return handleDtl(appApiTestDtlDao.get(aatdId));
	}

	/**
	 * 分页获取对象
	 * @param appApiTestDtl
	 * @return
	 */
	@Override
	public Page<AppApiTestDtl> pageQuery(AppApiTestDtl appApiTestDtl) {
		appApiTestDtl.setDefPageSize();
		int total = appApiTestDtlDao.findAppApiTestDtlCount(appApiTestDtl);
		List<AppApiTestDtl> data = null;
		if(total > 0) {
			data = appApiTestDtlDao.findAppApiTestDtl(appApiTestDtl);
			for (AppApiTestDtl item : data) {
				handleDtl(item);
			}
		}
		Page<AppApiTestDtl> page = new Page<AppApiTestDtl>(appApiTestDtl.getPage(), appApiTestDtl.getSize(), total, data);
		return page;
	}
	/**
	 * 处理字典等详细信息的方法
	 * @param appApiTestDtl
	 */
	private AppApiTestDtl handleDtl(AppApiTestDtl appApiTestDtl) {
		if (appApiTestDtl != null) {
			appApiTestDtl.setPrjName(prjInfoService.getName(appApiTestDtl.getPrjId()));
			appApiTestDtl.setPathName(prjApiService.getName(appApiTestDtl.getPrjId(), appApiTestDtl.getPath()));
		}
		return appApiTestDtl;
	}
	
	/**
	 * 根据aatdId删除对象
	 * @param aatdId
	 * @return
	 */
	@Override
	public ResponseFrame delete(String aatdId) {
		ResponseFrame frame = new ResponseFrame();
		appApiTestDtlDao.delete(aatdId);
		frame.setSucc();
		return frame;
	}

	@Override
	public List<AppApiTestDtl> findByAatId(String aatId) {
		return appApiTestDtlDao.findByAatId(aatId);
	}

	@Override
	public ResponseFrame test(String aatdId, String batchNo, SysUser user) {
		ResponseFrame frame = new ResponseFrame();
		AppApiTestDtl test = get(aatdId);
		if (test == null) {
			frame.setCode(-2);
			frame.setMessage("不存在测试实例");
			return frame;
		}
		PrjApi api = prjApiService.get(test.getPrjId(), test.getPath());
		if (api == null) {
			frame.setCode(-2);
			frame.setMessage("不存在api接口");
			return frame;
		}
		frame = apiTestUtil.post(test, api, batchNo, user);
		return frame;
	}

	@Override
	public void updateStatus(String aatdId, Integer status, String aatlId) {
		appApiTestDtlDao.updateStatus(aatdId, status, aatlId);
	}

	@Override
	public ResponseFrame testAll(String aatId, SysUser user) {
		ResponseFrame frame = new ResponseFrame();
		List<AppApiTestDtl> list = findByAatId(aatId);
		String batchNo = FrameTimeUtil.getStrTime();
		for (AppApiTestDtl appApiTestDtl : list) {
			test(appApiTestDtl.getAatdId(), batchNo, user);
		}
		frame.setSucc();
		frame.setMessage("共[" + list.size() + "]条接口测试完成!");
		return frame;
	}

	@Override
	public Map<String, Object> lookReport(String aatId) {
		Map<String, Object> data = new HashMap<>();
		List<String> batchList = appApiTestLogService.findBatchNoByAatId(aatId);
		List<String> batchNoResults = new ArrayList<String>();
		for (String batchNo : batchList) {
			List<AppApiTestLog> logList = appApiTestLogService.findByAatIdBatchNo(aatId, batchNo);
			int sum = logList.size();
			int succ = 0;
			int fail = 0;
			for (AppApiTestLog log : logList) {
				if (log.getStatus() == 30) {
					succ ++;
				} else if (log.getStatus() == 40) {
					fail ++;
				}
			}
			batchNoResults.add(batchNo + " 当前共测试" + sum + "个接口，成功" + succ + "，失败" + fail + "。" + (fail > 0 ? "具体失败原因点击对应失败的接口查看原因。":"") );
		}
		data.put("batchNoResults", batchNoResults);
		AppApiTest appApiTest = appApiTestService.get(aatId);
		List<AppUser> users = appUserService.findByAiId(appApiTest.getAiId());
		data.put("users", users);
		return data;
	}
}